﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.EstRec;
using System.Data;
using System.Windows;
using System.ComponentModel;

namespace BAL.EstRec
{
    public class CapturaHQ : INotifyPropertyChanged
    {
        public long unidHqBgtVersion { get; set; }
        public long unidHqBgt { get; set; }

        public string idPos { get; set; }
        public string POS { get; set; }
        public string idRecurso { get; set; }
        public string Recurso { get; set; }
        public int cantidad; //{ get; set; }
        private bool _isChecked;

        public int Cantidad
        {
            get { return cantidad; }
            set
            {
                if (value != cantidad)
                {
                    this.cantidad = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Cantidad"));
                    }

                }
            }
        }
        public bool IsChecked
        {
            get { return this._isChecked; }
            set
            {
                if (value != this._isChecked)
                {
                    this._isChecked = value;
                    if (this.PropertyChanged != null)
                        this.PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
                }
            }
        }


        public static List<CapturaHQ> GetCapturaHQ(long unidHqBgtVersion)
        {
            List<CapturaHQ> captura;
            System.Data.DataTable dt;
            string sqlStmt;

            //Cadena que ejeucta sp
            sqlStmt = "exec SP_ER_V050_GET_HQBGT "
                                + "@unidVersion=@@par1;";
            sqlStmt = sqlStmt.Replace("@@par1", unidHqBgtVersion.ToString());

            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                //Invertido
                captura = dt.AsEnumerable().Select(
                            row => new CapturaHQ
                            {
                                unidHqBgt = row.Field<long?>("UNID_HQBGT").GetValueOrDefault()
                                ,
                                idPos = String.IsNullOrEmpty(row.Field<string>("POS")) ? "" : row.Field<string>("POS")
                                ,
                                POS = String.IsNullOrEmpty(row.Field<string>("ID_POS")) ? "" : row.Field<string>("ID_POS")
                                ,
                                idRecurso = String.IsNullOrEmpty(row.Field<string>("RECURSO")) ? "" : row.Field<string>("RECURSO")
                                ,
                                Recurso = String.IsNullOrEmpty(row.Field<string>("ID_RECURSO")) ? "" : row.Field<string>("ID_RECURSO")
                                ,
                                Cantidad = row.Field<int?>("CANTIDAD").GetValueOrDefault()
                                ,
                                unidHqBgtVersion = row.Field<long?>("UNID_HQBGT_VERSION").GetValueOrDefault()
                            }
                        ).ToList();
                return captura;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return null;
        }

        public static void InsertCapturaHQ(CapturaHQ cap)
        {

            string sqlStmt;
            DataTable dt;
            //Invertidos
            //Cadena que ejeucta sp
            sqlStmt = "exec SP_INSERT_HQBGT "
                                + "@UNID_HQBGT_VERSION=@@par1"
                                + ",@UNID_HQBGT=@@par6"
                                + ",@POS='@@par2'"
                                + ",@RECURSO='@@par3'"
                                + ",@CANTIDAD=@@par4"
                                + ",@CONFIRMED=@@par5";
            sqlStmt = sqlStmt.Replace("@@par1", cap.unidHqBgtVersion.ToString());
            sqlStmt = sqlStmt.Replace("@@par2", cap.idPos.ToString());
            sqlStmt = sqlStmt.Replace("@@par3", cap.idRecurso.ToString());
            sqlStmt = sqlStmt.Replace("@@par4", cap.Cantidad.ToString());
            sqlStmt = sqlStmt.Replace("@@par5", "1");
            sqlStmt = sqlStmt.Replace("@@par6", cap.unidHqBgt.ToString());

            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                if (dt.Rows.Count == 1)
                {
                    //MessageBox.Show(Convert.ToString(dt.Rows[0]["UNID_HQ"]));
                }
                else
                {
                    //MessageBox.Show("Error al agregar datos...", "Mensaje Sistema", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static void DeleteCapturaHQ(List<string> aIdsHQ)
        {
            //convirto 
            //List<string> aid;
            string join;
            // forma el query
            string sqlStmt;
            // respuesta de la base de datos si borro los datos
            int res;

            if (aIdsHQ == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(", ", aIdsHQ);


            }

            // cadena que ejecuta el store

            sqlStmt = "exec SP_ER_v050_DELETE_DATOS_HQ "
                                + "@aIdsHQ='@@par1';";

            sqlStmt = sqlStmt.Replace("@@par1", join);

            try
            {
                res = SrvDB.ExecuteNonQuery(sqlStmt);
                if (res != 0)
                {
                    //MessageBox.Show("exito al borrar");
                }
                else
                {
                    MessageBox.Show("Error al borrar...", "Mensaje Sistema", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }


        }

        public static void UpdateCapturaHQ(CapturaHQ cap)
        {
            string sqlStmt;
            DataTable dt;

            //Cadena que ejeucta sp
            sqlStmt = "exec SP_ER_v050_UPDATE_DATOS_HQ "
                                + "@UNID_HQBGT_VERSION=@@par1"
                                + ",@UNID_HQBGT=@@par2"
                                + ",@POS='@@par3'"
                                + ",@RECURSO='@@par4'"
                                + ",@CANTIDAD=@@par5;";

            sqlStmt = sqlStmt.Replace("@@par1", cap.unidHqBgtVersion.ToString());
            sqlStmt = sqlStmt.Replace("@@par2", cap.unidHqBgt.ToString());
            sqlStmt = sqlStmt.Replace("@@par3", cap.idPos.ToString());
            sqlStmt = sqlStmt.Replace("@@par4", cap.idRecurso.ToString());
            sqlStmt = sqlStmt.Replace("@@par5", cap.Cantidad.ToString());


            try
            {
                dt = SrvDB.ExecuteQuery(sqlStmt);
                if (dt.Rows.Count == 0)
                {
                    //MessageBox.Show("exio al actualizar");
                }
                else
                {
                    MessageBox.Show("Error al actualizar", "Mensaje Sistema", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        public static void UpdateConfirmed(string Version)
        {
            string sqlStmt = "exec SP_UPDATE_CONFIRMED_HQBGT " + Version;
            try
            {
                SrvDB.ExecuteNonQuery(sqlStmt);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        //public static void UpdateConsolidacionHQ(CapturaHQ cap)
        public static void UpdateConsolidacionHQ(List<CapturaPivotFDV> list, string version)
        {
            int flushEvery = 500;

            int count = 0;

            string sqlStmt = "";

            DataTable dt;

            //Cadena que ejeucta sp

            string baseSqlStmt = "exec SP_ER_UPDATE_POS_RECURSO_HQ "
                                + "@UNID_HQBGT_VERSION=" + version
                                + ",@POS='@@par2'"
                                + ",@RECURSO='@@par3'"
                                + ",@CANTIDAD=@@par4;";

            foreach (CapturaPivotFDV cpf in list)
            {
                sqlStmt += baseSqlStmt.Replace(
                        "@@par2", cpf.pos.idPos.ToString()
                    ).Replace(
                        "@@par3", cpf.recurso.idRecurso.ToString()
                    ).Replace(
                        "@@par4", cpf.cantidad.ToString()
                    );
                sqlStmt += "; ";

                if (count == flushEvery)
                {
                    try
                    {
                        SrvDB.ExecuteNonQuery(sqlStmt);
                        sqlStmt = "";
                        count = 0;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                }

                count++;
            }
            if (!String.IsNullOrEmpty(sqlStmt))
            {
                try
                {
                    SrvDB.ExecuteNonQuery(sqlStmt);
                    sqlStmt = "";
                    count = 0;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public CapturaHQ() { }
        public CapturaHQ(long unidHqBgtVersion, string idPos, string idRecurso, int Cantidad)
        {
            this.unidHqBgtVersion = unidHqBgtVersion;
            this.idPos = idPos;
            this.idRecurso = idRecurso;
            this.Cantidad = Cantidad;
        }
        public CapturaHQ(long unidHqBgtVersion, string idPos, string POS, string idRecurso, string Recurso, int Cantidad)
        {
            //Invertidos
            this.unidHqBgtVersion = unidHqBgtVersion;
            this.idPos = POS;
            this.POS = idPos;
            this.idRecurso = Recurso;
            this.Recurso = idRecurso;
            this.Cantidad = Cantidad;
            this._isChecked = false;
        }
        public CapturaHQ(long unidHqBgtVersion, long unidHqBgt, string idPos, string idRecurso, int Cantidad)
        {
            this.unidHqBgtVersion = unidHqBgtVersion;
            this.unidHqBgt = unidHqBgt;
            this.idPos = idPos;
            this.idRecurso = idRecurso;
            this.Cantidad = Cantidad;
        }
        public CapturaHQ(string POS, string Recurso, int Cantidad)
        {
            this.POS = POS;
            this.Recurso = Recurso;
            this.Cantidad = Cantidad;
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}
